package com.net.core.core.sql;

import com.net.core.core.YXException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class BatchSql<T> {
    private static final Logger log = LoggerFactory.getLogger(BatchSql.class);
    private List<Object[]> batchArgs = new ArrayList();
    List<Field> sortArrayKey = new ArrayList();
    YxSqlPlus yxSqlPlus;
    ObjReflect listObj;
    List<T> listBean;

    public boolean isLog() {
        return this.yxSqlPlus.isLog();
    }

    public BatchSql(YxSqlPlus yxSqlPlus, List<T> list) {
        this.yxSqlPlus = yxSqlPlus;
        this.listObj = new ObjReflect(list.get(0));
        this.listBean = list;
    }

    public BatchSql append(String s) {
        this.yxSqlPlus.append(s);
        return this;
    }

    public BatchSql select(String s) {
        this.yxSqlPlus.select(s);
        return this;
    }

    public BatchSql select() {
        this.yxSqlPlus.select();
        return this;
    }

    public BatchSql eq(String column, String valueKey) {
        this.yxSqlPlus.eq(column, valueKey);
        return this;
    }

    public BatchSql<T> addValueKey(String key) {
        Field field = (Field) this.listObj.getFieldAll().get(key.toLowerCase());
        this.sortArrayKey.add(field);
        return this;
    }

    public BatchSql<T> updateKey(SFunction<T, ?>... byKey) {
        this.append("update").append(this.yxSqlPlus.tableName).append("set");

        for (int i = 0; i < byKey.length; ++i) {
            String column = LambdaUtils.getColumn(byKey[i]);
            this.append(column).append("=?,");
            Field field = (Field) this.listObj.getFieldAll().get(column.toLowerCase());
            this.sortArrayKey.add(field);
        }

        this.yxSqlPlus.delLast(",");
        this.append("where");
        return this;
    }

    public BatchSql<T> eqKey(SFunction<T, ?> byKey) {
        String column = LambdaUtils.getColumn(byKey);
        return this.eqKey(column);
    }

    public BatchSql<T> eqKey(String byKey) {
        this.yxSqlPlus.addOrAnd().append(byKey).append("=?");
        Field field = (Field) this.listObj.getFieldAll().get(byKey.toLowerCase());
        this.sortArrayKey.add(field);
        return this;
    }

    public String toSqlSingeTb() {
        return this.yxSqlPlus.sql.toString();
    }

    public <T> void insertBatch() {
        if (this.listBean != null && this.listBean.size() != 0) {
            Map<String, Field> fieldAll = this.listObj.getFieldAll();
            this.append("insert into ").append(this.yxSqlPlus.tableName);
            this.append("(");

            try {
                Iterator var2 = fieldAll.entrySet().iterator();
                while (var2.hasNext()) {
                    Entry<String, Field> item = (Entry) var2.next();
                    Field field = (Field) this.listObj.getFieldAll().get(item.getKey().toLowerCase());
                    TableId anno = field.getAnnotation(TableId.class);
                    if (anno != null && anno.autoincrement()) {
                        continue;
                    }
                    this.yxSqlPlus.param(item.getKey()).append(",");
                    this.sortArrayKey.add(field);
                }
            } catch (Exception var5) {
                throw new YXException(var5.getMessage(), var5);
            }

            this.yxSqlPlus.delLast(",");
            this.append(") values (");

            for (int i = 0; i < this.sortArrayKey.size(); ++i) {
                this.append("?,");
            }

            this.yxSqlPlus.delLast(",");
            this.append(")");
        }
    }

    public <T> void deleteBatchById() {
        if (this.listBean != null && this.listBean.size() != 0) {
            Map<String, Field> ids = this.listObj.getTableIds();
            if (ids.size() == 0) {
                throw new YXException("请配置 TableId");
            } else {
                this.append("DELETE FROM").append(this.yxSqlPlus.tableName).append("WHERE");

                try {
                    Iterator var2 = ids.entrySet().iterator();

                    while (var2.hasNext()) {
                        Entry<String, Field> entry = (Entry) var2.next();
                        entry.getValue().setAccessible(true);
                        this.append(entry.getKey()).append("=? and");
                        this.sortArrayKey.add(entry.getValue());
                    }

                    this.yxSqlPlus.delLast("and");
                } catch (Exception var4) {
                    throw new YXException(var4);
                }
            }
        }
    }

    public Object[] getBatchDataArray() {
        List<Object> list = new ArrayList();
        List<Object[]> batchData = this.getBatchData();

        for (int i = 0; i < batchData.size(); ++i) {
            for (int j = 0; j < ((Object[]) batchData.get(i)).length; ++j) {
                list.add(((Object[]) batchData.get(i))[j]);
            }
        }

        return list.toArray();
    }

    public List<Object[]> getBatchData() {
        this.batchArgs.clear();
        Iterator var1 = this.listBean.iterator();

        while (var1.hasNext()) {
            Object obj = var1.next();
            List<Object> value = new ArrayList();
            Iterator var4 = this.sortArrayKey.iterator();

            while (var4.hasNext()) {
                Field field = (Field) var4.next();

                try {
                    field.setAccessible(true);
                    value.add(field.get(obj));
                } catch (IllegalAccessException var7) {
                    var7.printStackTrace();
                }
            }

            this.batchArgs.add(value.toArray());
        }

        return this.batchArgs;
    }

    public String toSql() {
        String s = this.yxSqlPlus.toSql();
        return s;
    }

    public String toSelectSql() {
        String s = this.yxSqlPlus.toSql();
        StringBuffer stringBuffer = new StringBuffer(s);

        for (int i = 1; i < this.listBean.size(); ++i) {
            stringBuffer.append(" union ").append(s);
        }

        return stringBuffer.toString();
    }

    public void updateBatch() {
    }
}
